/** Progetto PRAISED **/
package it.uniroma3.dia.praised.util;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URL;

import org.apache.pdfbox.cos.COSDocument;
import org.apache.pdfbox.pdfparser.PDFParser;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDDocumentInformation;
import org.apache.pdfbox.util.PDFTextStripper;

/**
 * PDFTextParser : This class extract the text in a PDF file and store it into a text file. 
 *  
 * @author Accosta Augusto
 * @author Lopez Elisabetta
 * @version 1.0
 *
 */
public class PDFTextParser {
	
	private static final String PATH = "repository/xml/"; //Percorso in cui verr� salvato il file di testo
	private PDFParser parser; //Parser del PDF
	private String parsedText; //Testo estratto dal PDF
	private PDFTextStripper pdfStripper; //Stripper del testo
	private PDDocument pdDoc; //Documento che rappresenta il file PDF
	private COSDocument cosDoc;
	
	/* Costruttore no-arg */
	public PDFTextParser() {
	}

	/**
	 * This method extrace the text of a PDF file from a specified URL
	 * 
	 * @param urlFile - PDF url
	 * @return parsedText - The PDF file text content.
	 */
	public String pdftoText(URL urlFile) {

		/* Apertura dello streaming verso il pdf */ 
		try {
			parser = new PDFParser(urlFile.openStream());
		} catch (Exception e) {
			System.out.println("Unable to open PDF Parser.");
			return null;
		}
		
		/* Recupero del testo contenuto all'interno del PDF e memorizzazione dello stesso in una stringa */
		try {
			parser.parse();
			cosDoc = parser.getDocument();
			pdfStripper = new PDFTextStripper();
			pdDoc = new PDDocument(cosDoc);
			parsedText = pdfStripper.getText(pdDoc);

		} catch (Exception e) {
			e.printStackTrace();
			try {
				if (cosDoc != null)
					cosDoc.close();
				if (pdDoc != null)
					pdDoc.close();
			} catch (Exception e1) {
				e.printStackTrace();
			}
			return null;
		}
		return parsedText;
	}

	
	/**
	 * This method store the PDF content into a text file.
	 * 
	 * @param pdfText - PDF contents
	 * @param fileName - The file name that host the PDF contents
	 */
	public void writeTextToFile(String pdfText, String fileName) {

		System.out.println("\nWriting PDF text to output text file " + fileName + " ....");
		try {
			PrintWriter pw = new PrintWriter(PATH + fileName);
			pw.print(pdfText);
			pw.close();
		} catch (Exception e) {
			System.out.println("An exception occured in writing the pdf text to file.");
			e.printStackTrace();
		}
		System.out.println("Done.");
	}

	/**
	 * This method extract the information of a PDF file from the specified URL
	 * 
	 * @param urlDoc - PDF url
	 */
	public void getInformationDocument(URL urlDoc) {
		PDDocument doc;
		try {
			doc = PDDocument.load(urlDoc);

			PDDocumentInformation info = doc.getDocumentInformation();
			System.out.println("Page Count=" + doc.getNumberOfPages());
			System.out.println("Title=" + info.getTitle());
			System.out.println("Author=" + info.getAuthor());
			System.out.println("Subject=" + info.getSubject());
			System.out.println("Keywords=" + info.getKeywords());
			System.out.println("Creator=" + info.getCreator());
			System.out.println("Producer=" + info.getProducer());
			System.out.println("Creation Date=" + info.getCreationDate());
			System.out.println("Modification Date=" + info.getModificationDate());
			System.out.println("Trapped=" + info.getTrapped());
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
